<?php
/**
 * Created by PhpStorm.
 * User: Apache
 *   文件调查。
 *
 */

namespace app\api\controller\basemanage;
use app\api\controller\common\ApiBase;
use think\Db;

class Store extends ApiBase
{

    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->admin = session('admin');
        //$this->model = model('CompanyPublic');
    }


    /**
     *
     * @return \think\response\Json|void
     *  comppub
     *
     */

    public function getList(){
        $page = $this->request->param('pageIndex');
        $keyword = $this->request->param('title');
        $city = $this->request->param('city');
        $page = max(1,intval($page));
        $pageSize =$this->request->param('pageSize')==""?20:$this->request->param('pageSize');
        $param=" and s.is_delete=0";
        if (!empty($keyword)) {
            $param.=" and s.title like '%$keyword%' ";
        }
        if(!empty($city)){
            $param.=" and s.city_id=$city ";
        }
        $sql2="select s.*,c.title as city_name from crm_store as s ";
        $sql2.="left join crm_city as c on c.id=s.city_id";
        $sql2.=" where 1=1 $param";
        $sql2.=" order by s.sort desc";
        $total1=Db::query($sql2);
        $sql2.=" limit ".($page-1)*$pageSize.",$pageSize";
        $list=Db::query($sql2);
        $total=count($total1); //st
        return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'total'=>$total]]);
    }


    /**
     * 添加
     * @return \think\response\Json|void
     *  zs_question       zs_question_option
     *  zs_question_type
     */
    public function add(){
        $data['create_time']=time();
        $data['update_time']=time();
        $data['title']=$this->request->param('title');   // 1单选 2多选3  填空
        $data['sort']=$this->request->param('sort');   // 1单选 2多选3  填空
        $data['status']=$this->request->param('status');
        $data['cover']=$this->request->param('cover');
        $data['city_id']=$this->request->param('city_id');
        $data['contact']=$this->request->param('contact');
        $data['mobile']=$this->request->param('mobile');
        $data['tel']=$this->request->param('tel');
        $data['province']=$this->request->param('province');
        $data['area_city']=$this->request->param('area_city');
        $data['area_town']=$this->request->param('area_town');
        $data['street']=$this->request->param('street');
        $data['lat']=$this->request->param('lat');
        $data['lng']=$this->request->param('lng');
        $data['address']=$this->request->param('address');
        $data['do_business_start']=$this->request->param('do_business_start');
        $data['do_business_end']=$this->request->param('do_business_end');
        $res=db('store')->insert($data);
        if(!$res){
            return json(['code'=>0,'msg'=>'操作失败','data'=>'']);
        }
        return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
    }

    /**
     * 删除
     */
    public function del(){
        $id = $this->request->param('ids');
        $ids = explode(',',$id);
        if(empty($id)){
            return json(['code'=>0,'msg'=>'参数有误','data'=>'']);
        }
        $msg='';
        $flag=true;
        foreach ($ids as $id){
            $r = db('store')->where(['id'=>$id])->find();
            if(!$r){
                $flag=false;
                $msg='id:'.$id.'记录不存在';
                break;
            }
        }

        if(!$flag){
            return json(['code'=>0,'msg'=>$msg]);
        }

        foreach ($ids as $id){
            $r = db('store')->where(['id'=>$id])->update(['is_delete'=>1]);
            if(!$r){
                $flag=false;
                $msg='id:'.$id.'删除失败请重试';
                break;
            }
        }

        if(!$flag){
            return json(['code'=>0,'msg'=>$msg]);
        }
        return json(['code'=>1,'msg'=>'操作成功']);

    }


    /**
     * 基本信息修改
     * @return \think\response\Json|void
     */
    public function edit()
    {
//        die();
        $id = $this->request->param('id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数有误，请核查']);
        }
        $data['update_time']=time();
        $data['title']=$this->request->param('title');   // 1单选 2多选3  填空
        $data['sort']=$this->request->param('sort');   // 1单选 2多选3  填空
        $data['status']=$this->request->param('status');
        $data['cover']=$this->request->param('cover');
        $data['city_id']=$this->request->param('city_id');
        $data['contact']=$this->request->param('contact');
        $data['mobile']=$this->request->param('mobile');
        $data['tel']=$this->request->param('tel');
        $data['province']=$this->request->param('province');
        $data['area_city']=$this->request->param('area_city');
        $data['area_town']=$this->request->param('area_town');
        $data['street']=$this->request->param('street');
        $data['lat']=$this->request->param('lat');
        $data['lng']=$this->request->param('lng');
        $data['address']=$this->request->param('address');
        $data['do_business_start']=$this->request->param('do_business_start');
        $data['do_business_end']=$this->request->param('do_business_end');
        $where['id'] = $id;
        $r=db('store')->where($where)->update($data);
        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }
        return json(['code' => 1, 'msg' => '操作成功']);
    }

    /**
     * 基本信息修改
     * @return \think\response\Json|void
     */
    public function getDetail()
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数有误，请核查']);
        }
        $where['id'] = $id;
        $r=db('store')->where($where)->find();
        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1, 'msg' => '操作成功','data'=>$r]);
    }

    /**
     * 基本信息修改
     * @return \think\response\Json|void
     */
    public function cityList()
    {
        $where['is_delete'] = 0;
        $r=db('city')->where($where)->select();
        return json(['code' => 1, 'msg' => '操作成功','data'=>$r]);
    }

}
